//
//  PointViewControllerViewController.m
//  jinnengExam
//
//  Created by mac on 2020/8/17.
//  Copyright © 2020 jznrj. All rights reserved.
//

#import "PointViewControllerViewController.h"
#import "IntroduceViewController.h"
#import "RankViewController.h"
#import "PBFlatBarButtonItems.h"
#import "GoalViewController.h"
@interface PointViewControllerViewController ()

@end

@implementation PointViewControllerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"个人积分";
    UINavigationBar * bar = self.navigationController.navigationBar;
    UIImage *bgImage = [ColorUtil navigationBarColor:[ColorUtil titleColor] BarSize:bar.frame alphe:1.0];
    [bar setBackgroundImage:bgImage forBarMetrics:UIBarMetricsDefault];
    [bar setTintColor:[UIColor whiteColor]];
    [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]}forState:UIControlStateNormal];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil]];
    
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    
    self.tabBarController.tabBar.frame = CGRectMake(0, 0, 0, 0);
     self.tabBarController.tabBar.hidden = YES;
    // Do any additional setup after loading the view.
    
    UIBarButtonItem *otherButton = [PBFlatBarButtonItems Title:@"获取情况" Target:self selector:@selector(toGoal)];
    [self.navigationItem setRightBarButtonItem:otherButton];
    [self initView];
}
-(void) viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden =NO;
}
-(void) initView{
   [self.view addSubview:self.jznWebView];
    [self.jznWebView mas_makeConstraints:^(MASConstraintMaker *make) {
         make.left.right.equalTo(self.view);
         make.top.bottom.equalTo(self.view);
     }];
    self.jznWebView.scrollView.bounces = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    [self.jznWebView loadLocalFile:@"/ios_jinneng/point" type:@"html"];
    
    [self registerJsFunction];
}

-(void) registerJsFunction{
    [self.webViewBridge registerHandler:@"toIntroduce" handler:^(id data, WVJBResponseCallback responseCallback) {
        dispatch_async(dispatch_get_main_queue(), ^(){
             IntroduceViewController *vc = [[IntroduceViewController alloc]init];
             [self.navigationController pushViewController:vc animated:YES];
         });
    }];
    
    [self.webViewBridge registerHandler:@"toRank" handler:^(id data, WVJBResponseCallback responseCallback) {
        dispatch_async(dispatch_get_main_queue(), ^(){
              RankViewController *vc = [[RankViewController alloc]init];
              [self.navigationController pushViewController:vc animated:YES];
      });
    }];
    
    [self.webViewBridge registerHandler:@"toLook" handler:^(id data, WVJBResponseCallback responseCallback) {
        int configId = [data[@"configId"] intValue];
        dispatch_async(dispatch_get_main_queue(), ^(){
            switch (configId) {
                case 2:
                    self.tabBarController.selectedIndex = 2;
                    [self.navigationController popToRootViewControllerAnimated:YES];
                    break;
                case 3:
                    self.tabBarController.selectedIndex = 1;
                    [self.navigationController popToRootViewControllerAnimated:YES];
                break;
                case 4:
                case 5:
                case 6:
                case 7:
                case 10:
                    self.tabBarController.selectedIndex = 0;
                    [self.navigationController popToRootViewControllerAnimated:YES];
                    break;
                default:
                    break;
            }
      });
    }];
}
-(void) toGoal{
    GoalViewController *vc = [[GoalViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
    [self requestPoint];
}
-(void) requestPoint{
    NSString *url = [NSString stringWithFormat:@"%@/userRank/findUserRankInfo" ,[URLUtil hostName]];
    NSDictionary *params = @{@"userId":[NSString stringWithFormat:@"%d",self.loginDto.userId]};
    [JZNRequestUtil requestUrl:url requestParams:params success:^(DataResult *dataResult) {
       if(dataResult.code == 0){
           
           NSString *clear = [NSString stringWithFormat:@"getPoint(%@)",[dataResult.data mj_JSONString]];
           [self.jznWebView evaluateJavaScript:clear completionHandler:nil];
       }else {
       }
    } failure:^(NSError *error) {
           
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
